import EditableCell from "./EditableCell";
import type { BasicColumn } from "@/components/Table/types/table";
import { isArray } from "@/utils/is";
import React from "react";

// interface Params {
// 	text: string;
// 	record: Recordable;
// 	index: number;
// }

export function renderEditCell(column: BasicColumn) {
	console.log("column", column);
	const EditCellRenderer = (value: string, record: Recordable, index: number) => {
		record.onValid = async () => {
			if (isArray(record?.validCbs)) {
				const validFns = (record?.validCbs || []).map(fn => fn());
				const res = await Promise.all(validFns);
				return res.every(item => !!item);
			} else {
				return false;
			}
		};

		record.onEdit = async (edit: boolean, submit = false) => {
			if (!submit) record.editable = edit;

			if (!edit && submit) {
				if (!(await record.onValid())) return false;
				const res = await record.onSubmitEdit?.();
				if (res) {
					record.editable = false;
					return true;
				}
				return false;
			}
			// cancel
			if (!edit && !submit) record.onCancelEdit?.();

			return true;
		};

		return React.createElement(EditableCell, {
			value,
			record,
			column,
			index
		});
	};
	return EditCellRenderer;
}

export type EditRecordRow<T = Recordable> = Partial<
	{
		onEdit: (editable: boolean, submit?: boolean) => Promise<boolean>;
		onValid: () => Promise<boolean>;
		editable: boolean;
		onCancel: Fn;
		onSubmit: Fn;
		submitCbs: Fn[];
		cancelCbs: Fn[];
		validCbs: Fn[];
		editValueRefs: Recordable;
	} & T
>;
